/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.util.List;
import javax.persistence.Query;
import model.pojo.Aluno;
import view.TelaPrincipal;

/**
 *
 * @author Samuel
 */
public class AlunoDaoImpl implements AlunoDao {

    @Override
    public void salvar(Aluno aluno) {
        TelaPrincipal.em.getTransaction().begin();
        TelaPrincipal.em.persist(aluno);
        TelaPrincipal.em.getTransaction().commit();
    }

    @Override
    public Aluno carregarAlunoPorMatricula(long matricula) {
        Aluno aluno;
        TelaPrincipal.em.getTransaction().begin();
        Query query = TelaPrincipal.em.createQuery("from aluno aluno where aluno.matricula = :matricula");
        query.setParameter("matricula", matricula);
        aluno = (Aluno) query.getSingleResult();
        TelaPrincipal.em.getTransaction().commit();
        return aluno;
    }

    @Override
    public List<Aluno> carregarAlunoPorNome(String nome) {
        Aluno aluno;
        List lista;
        TelaPrincipal.em.getTransaction().begin();
        Query query = TelaPrincipal.em.createQuery("from aluno aluno where aluno.nome = :nome");
        query.setParameter("nome", nome);
        lista = query.getResultList();
        TelaPrincipal.em.getTransaction().commit();
        return lista;
    }
    
}
